<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<?php
if (! $_POST['unit'] ) {
	header('Location: intro.php');
}
?>
<head>
	<title>Introduction to Attic Greek</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="flashcardv.css" rel="stylesheet" type="text/css">
</head>

<?php

try {
	$db = new PDO('sqlite:Mastronarde.db3');

	$n = $_POST['unit'];
	
	$gks = array();
	$q = $db->query("SELECT DISTINCT greek FROM gkforms WHERE unit=$n;");
	$gks = $q->fetchAll(PDO::FETCH_NUM);
	
	$q = $db->query("SELECT DISTINCT greek FROM gkforms WHERE unit < $n AND greek NOT IN ('".implode("', '", $gks)."') ORDER BY RANDOM() LIMIT 15;");
	$gks = array_merge($gks, $q->fetchAll(PDO::FETCH_NUM));
	
	$words = array();
	foreach ($gks as $gka) {
		$gk = $gka[0];
		$q = $db->query("SELECT DISTINCT greek, voc1, voc2, answer FROM gkforms WHERE unit <= $n AND greek='$gk';");
		$tmp = $q->fetchAll(PDO::FETCH_NUM);
		$voc1 = '';
		$voc2 = '';
		$aw = '';
		for($i = 0; $i < count($tmp); $i++) {
			$greek = $tmp[$i][0];
			if ($voc1) {
				$voc1 .= '<br />';
			}
			if ($voc2) {
				$voc2 .= '<br />';
			}
			if ($aw) {
				$aw .= '<br />';
			}
			$vn = '';
			if (count($tmp) > 1) {
				$vn = ($i + 1).". ";
			}
			$voc1 .= $vn.$tmp[$i][1];
			$voc2 .= $vn.$tmp[$i][2];
			$aw .= $vn.$tmp[$i][3];
		}
		$words[] = array('gfirst', $greek, $voc1, $voc2, $aw);
	}
	shuffle($words);
	
	$q = $db->query("SELECT DISTINCT eng, voc1, voc2, greek FROM enggrk WHERE unit < $n ORDER BY RANDOM() LIMIT 15;");
	$tmp = $q->fetchAll(PDO::FETCH_NUM);
	foreach($tmp as $ita) {
		$words[] = array('efirst', $ita[0], $ita[1], $ita[2], $ita[3]);
	}
	
	$q = $db->query("SELECT DISTINCT eng, voc1, voc2, greek FROM enggrk WHERE unit = $n ORDER BY RANDOM();");
	$tmp = $q->fetchAll(PDO::FETCH_NUM);
	foreach($tmp as $ita) {
		$words[] = array('efirst', $ita[0], $ita[1], $ita[2], $ita[3]);
	}
	
	
	
	
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<script type="text/javascript">
<?php
print "var words = new Array(".count($words).");\n";
for($i = 0; $i < count($words); $i++) {
	$tmp = implode("', '", $words[$i]);
	print "words[$i] = ['$tmp'];\n";
}
?>

var shown = 0;

function updateVoc() {
	var i = Math.floor(shown / 4);

	var j = shown % 4;
	var w0, w2;
	if (words[i][0] == "gfirst") {
		document.getElementById("v1").className = "gk";
		document.getElementById("v3").className = "v3";
	} else {
		document.getElementById("v1").className = "v3";
		document.getElementById("v3").className = "gk";
	}

	if (j == 0) {
		document.getElementById('v1').innerHTML = words[i][1];
		document.getElementById('v2').innerHTML = "";
		document.getElementById('v3').innerHTML = "";
	} else if (j == 1) {
		document.getElementById('v1').innerHTML = words[i][1];
		document.getElementById('v2').innerHTML = words[i][2];
		document.getElementById('v3').innerHTML = "";
	} else if (j == 2) {
		document.getElementById('v1').innerHTML = words[i][1];
		document.getElementById('v2').innerHTML = words[i][3];
		document.getElementById('v3').innerHTML = "";
	} else if (j == 3) {
		document.getElementById('v1').innerHTML = words[i][1];
		document.getElementById('v2').innerHTML = '&nbsp;';
		document.getElementById('v3').innerHTML = words[i][4];
	}
}

function next() {
	shown++;
	if (shown > words.length * 4 - 1) {
		shown = 0;
		alert('All done!');
	}
	updateVoc();
}

function prev() {
	shown--;
	if (shown < 0) {
		shown = words.length * 4 - 1;
		alert('All done!');
	}
	updateVoc();
}


</script>

<body onload="updateVoc()">
<script language="javascript" type="text/javascript">
function playSound(soundfile) {
	document.getElementById("dummy").innerHTML="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}
</script>

<h1 style="text-align:center">Introduction to Attic Greek</h1>



<div class="center">

<div id="v1" class="v1"></div>
<div id="v2" class="v2">&nbsp;</div>
<div id="v3" class="gk"></div>

<a href="#" class="button" id="btnprev" onclick="prev()" accesskey="p">&lt;&lt;</a>
<a href="#" class="button" id="btnnext" onclick="next()" accesskey="n">&gt;&gt;</a>
<a href="#" class="button" id="btnnew" onclick="location.reload();">new</a>


</div>


<span id="dummy"></span>
</body>
</html>
